package com.zhiyizhen.handheld.net;


import com.zhiyizhen.handheld.common.LogUtil;
import com.zhiyizhen.handheld.common.ToastUtil;
import com.zhiyizhen.handheld.net.util.NetworkUtils;

import io.reactivex.observers.DisposableObserver;

public abstract class BaseObserver<T> extends DisposableObserver<T> {

    @Override
    public final void onComplete() {
        onFinally();
        if (!isDisposed()) {
            dispose();
        }
    }

    @Override
    public final void onError(Throwable e) {
        if (NetworkUtils.isDebug()) {
            LogUtil.e(e);
        }
        if (!(e instanceof ResponseException)) {
            LogUtil.e(e);
            e = new ResponseException();
        }

        ResponseException exception = (ResponseException) e;
        onFailure(exception);
        onFinally();
        if (!isDisposed()) {
            dispose();
        }
        NetworkUtils.getInterceptor().onResponseError(exception);
    }

    protected void onFailure(ResponseException e) {
        ToastUtil.showShort(e.getMsg());
    }

    protected void onFinally() {

    }

}
